package com.aproj.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.aproj.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Override
	public User getUserByEmailAndPassword(final String email, final String password) {
		
		final Md5PasswordEncoder encoder = new Md5PasswordEncoder();

		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createCriteria(User.class).add(Restrictions.eq("email", email))
						.add(Restrictions.eq("password", password))
						.list();
			}
		});
		
		if(list.isEmpty()){
			return null;
		}
		
		return (User) list.get(0);
	}
	
}